function newmap = cellShifter(map, xy, spacing);
% CELLSHIFTER
%
% syntax:   newmap = cellShifter(map, xy, spacing)
%
% Takes in a cell's map and centers it on the soma position (or any XY position,
% such representing the center of the barrel/septal pattern), and 
% pads the edges with NaNs.

% editing: gs 2003, 2004

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% (check that soma is within mapped area?)

% initialize a new map, twice the width of the original
[r,c,p] = size(map);
newmap = zeros(2*r, 2*c, p) * NaN;

for n=1:p % go through the maps one by one
    
    % calculate how many rows and cols to shift by
    % (note that x corresponds to rows, y to cols y Matlab convention)
    shiftRows = -round(xy(n,2)/spacing);
    shiftCols = -round(xy(n,1)/spacing);
    
    % place the original map onto the new map, with shift
    newmap(1 + round(r/2) + shiftRows: r + round(r/2) + shiftRows, ...
           1 + round(c/2) + shiftCols: c + round(c/2) + shiftCols, n) = map(:,:,n);
end
